﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace Common
{
   public class GetOption
    {
       static XmlDocument doc = new XmlDocument();

       static GetOption()
       {
           doc.Load(AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"Config\\AppOptions.xml");
       }

       public static string GetConfigInnerText(string configName)
       {
           XmlNode xn = doc.SelectSingleNode(String.Format("//Options//{0}", configName));
           return xn.InnerText;
       }

       public static Dictionary<string, string> GetConfigDict(string configName)
       {
           XmlNode xn = doc.SelectSingleNode(String.Format("//Options//{0}", configName));
           Dictionary<string, string> dic = new Dictionary<string, string>();
           foreach (XmlNode xnChild in xn.ChildNodes)
           {
               if (xnChild.NodeType != XmlNodeType.Comment)//忽略注释
               {
                   dic.Add(xnChild.Attributes["key"].Value, xnChild.Attributes["value"].Value);
               }
           }
           return dic;
       }

       public static string GetConfigSigleValue(string configName, string key)
       {
           XmlNode xn = doc.SelectSingleNode(String.Format("//Options//{0}", configName));
           foreach (XmlNode xnChild in xn.ChildNodes)
           {
               if (xnChild.NodeType != XmlNodeType.Comment)//忽略注释
               {
                   if (xnChild.Attributes["key"].Value == key)
                   {
                       return xnChild.Attributes["value"].Value;
                   }
               }
           }
           return "";
       }
    }
}
